#ifndef __SURFACE_H__
#define __SURFACE_H__

#include <d3d9.h>

//#include "Global.h"
#include "Trace.h"


/**--------------------------------------------------------
*Program/Project : GameMario
*Written by : 
*Email :
*Created date : 7/10/2012
*Modified by :
*Modified date :
*Version : 1.0
*Description : Lop khoi tao Surface
--------------------------------------------------------**/


class SURFACE
{
public:
	LPDIRECT3DDEVICE9		d3ddv;			//Direct3D device
	LPDIRECT3DSURFACE9		BackBuffer;	// The buffer of direct3d
	LPWSTR FileName;
	LPDIRECT3DSURFACE9 Surface;

	SURFACE(LPDIRECT3DDEVICE9 _d3ddv, LPDIRECT3DSURFACE9 _BackBuffer, LPWSTR _fileName)
	{
		d3ddv = _d3ddv;
		BackBuffer = _BackBuffer;
		FileName = _fileName;
		this->Load();
	}

	~SURFACE()
	{
		if(this->Surface)
		{
			this->Surface->Release();
		}
	}

	void Draw(const RECT* sRect, const RECT* dRect)  
	{ 
		d3ddv->StretchRect(
			this->Surface,		// from , gameworld 
			sRect,				// which portion?
			BackBuffer,		// to , backbuffer
			dRect,				// which portion?
			D3DTEXF_NONE);
	}

	void Load()
	{
		D3DXIMAGE_INFO info; 

		HRESULT result = D3DXGetImageInfoFromFile(FileName, &info);
		if (result != D3D_OK)
		{
			trace(L"[ERROR] Failed to get image info ");
			return;
		}

		d3ddv->CreateOffscreenPlainSurface(
			info.Width,				//width of the surface
			info.Height,			//height of the surface
			D3DFMT_X8R8G8B8,		//surface format
			D3DPOOL_DEFAULT,		//memory pool to use
			&Surface,				//pointer to the surface
			NULL);					//reserved (always NULL)

		result = D3DXLoadSurfaceFromFile(
			Surface, 					// destanation surface
			NULL,						// destination palette	
			NULL,						// destination rectangle 
			FileName,					// source filename
			NULL,						// source rectangle
			D3DX_DEFAULT, 				// filter image
			D3DCOLOR_XRGB(0,0,0),		// transparency (0 = none)
			NULL);						// source image info (usually NULL)

		if (result != D3D_OK)
		{
			trace(L"[ERROR] D3DXLoadSurfaceFromFile() failed");
			return;
		}
	} 
};

#endif